# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html

import json
import codecs

import pymysql


class Jobin51SpiderPipeline(object):

    def __init__(self):
        self.file = codecs.open('job.json', 'w', encoding='utf-8')

    def process_item(self, item, spider):

        # print('职位:', item['jobPosition'])
        # print('公司:', item['jobCompany'])
        # print('工作地点:', item['jobArea'])
        # print('薪资:', item['jobSale'])
        # print('发布时间:', item['jobDate'])
        # print('----------------------------')
        line = json.dumps(dict(item), ensure_ascii=False) + "\n"
        self.file.write(line)
        return item

    def spider_closed(self, spider):
        self.file.close()


class job51MySQLPipeline(object):
    """docstring for MySQLPipeline"""

    # def __init__(self):
    #    self.connpool = pymysql.connect(host='101.132.137.72',
    #                                    port=3306,
    #                                     user='root',
    #                                   password='wxe',
    #                                    db='recruitment',
    #                                 charset='utf8')

    def process_item(self, item, spider):
        self.connpool = pymysql.connect(host='101.132.137.72',
                                        port=3306,
                                        user='root',
                                        password='wxe',
                                        db='recruitment',
                                        charset='utf8')
        print("进入数据库存储")
        # 得到一个可以执行SQL语句的光标对象
        cursor = self.connpool.cursor()  # 执行完毕返回的结果集默认以元组显示
        if item.get('jobPosition') and item.get('jobCompany') and item.get('jobArea') and item.get('jobSale') and \
                item.get('jobKeyword') and item.get('jobEducation') and item.get('jobExperience'):
            try:
                print("更新语句")
                print(item['jobPosition'])
                cursor.execute(
                    "insert into job_info (position, url, company, adress, pay, pubdate, keyword, education, experience)"
                    " values(%s, %s, %s, %s, %s, %s, %s, %s, %s)",
                    (item['jobPosition'], item['jobUrl'], item['jobCompany'], item['jobArea'], item['jobSale'],
                     item['jobDate'], item['jobKeyword'], item['jobEducation'], item['jobExperience']))
                self.connpool.commit()
            except MemoryError:
                print("cuowu")

        return item

    def handle_error(self, e):
        log.err(e)